package cn.fastoo.web.backstage.web.controller.excel;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.fastoo.common.dao.member.MemberDao;
import cn.fastoo.common.entity.member.Member;
import cn.fastoo.common.sampleframe.dto.JsonResult;
import cn.fastoo.web.backstage.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;



@Controller
@RequestMapping("fileExcel")
public class excelFileUploadsController {

	@Autowired
	private MemberDao memberDao;
	//导入excel
	@RequestMapping(value = "/import", method=RequestMethod.POST)
	@ResponseBody
	public String importExcel(@RequestParam(value="file",required = false) MultipartFile file, HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		String result = readExcelFile(file);
		map.put("message", result);
		return JsonResult.success("成功");
	}

	public String readExcelFile(MultipartFile file) throws Exception{
		String result = "";
		//创建处理EXCEL的类
		ReadExcel readExcel = new ReadExcel();
		//解析excel，获取上传的事件单
		List<Map<String, Object>> userList = readExcel.getExcelInfo(file);
		//至此已经将excel中的数据转换到list里面了,接下来就可以操作list,可以进行保存到数据库,或者其他操作,
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for(Map<String, Object> user:userList){
			String sex = user.get("sex").toString();
			String status = user.get("status").toString();
			if(status.equals("意向客户")){
				status = "0";
			}else if(status.equals("无意向客户")){
				status = "1";
			}else if(status.equals("潜在客户")){
				status = "2";
			}else{
				status = "3";
			}
			if(sex.equals("男")){
				sex ="0";
			}else if(sex.equals("女")){
				sex ="1";
			}
			Member member =new Member();
			member.setUsername(user.get("username").toString());
			member.setCollectTime(sdf.parse(user.get("collectTime").toString()));
			member.setBirthday(sdf.parse(user.get("birthday").toString()));
			member.setOriginDate(sdf.parse(user.get("originDate").toString()));
			member.setEnglishName(user.get("englishName").toString());
			member.setSex(Integer.valueOf(sex));
			member.setIntroducer(user.get("introducer").toString());
			member.setIsSchool(user.get("isSchool").toString());
			member.setIdCard(user.get("idCard").toString());
			member.setParent(user.get("parent").toString());
			member.setHandset(user.get("handset").toString());
			member.setWechat(user.get("wechat").toString());
			member.setAddress(user.get("address").toString());
			member.setCampus(user.get("campus").toString());
			member.setNationality(user.get("nationality").toString());
			member.setActivityLocation(user.get("activityLocation").toString());
			member.setCollectPeople(user.get("collectPeople").toString());
			member.setConsultantGetMan(user.get("consultantGetMan").toString());
			member.setOriginNotes(user.get("originNotes").toString());
			member.setRemark(user.get("remark").toString());
			member.setStatus(Integer.valueOf(status));
			member.setParentTwo(user.get("parentTwo").toString());
			member.setHandsetTwo(user.get("handsetTwo").toString());
			memberDao.insert(member,ShiroUtils.getUserId(),ShiroUtils.getName());
		}
		if(userList != null && !userList.isEmpty()){
			result = "上传成功";
		}else{
			result = "上传失败";
		}
		return result;
	}



}
